package battleModel;

import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Tank class, AI to follow...
 * Obstacle Attributes:
 *     health=100, damageable=true, location=in constructor, image=in constructor
 * Other attributes:
 *     angle=in constructor, speed=in constructor (4 is average, 0-10 is immobile to fast);
 *     
 * Default Tank, as a result of Tank() constructor has:
 * angle=0,speed=3,image=default Tank, location=(300,300)
 */
public class JakesTank extends Obstacle{
	
	private double speed;
	
	private final double tankCenterX = 31.5, tankCenterY =31.5;
	private final double turretCenterX = 31.5, turretCenterY = 30.5;
//	private final double tankCenterX = 51.5, tankCenterY =33;
//	private final double turretCenterX = 49, turretCenterY = 18;
	 
	private Perimeter perimeter;
	private volatile double tankAngle, turretAngle;
	private AffineTransform tankAffine, turretAffine;
	private Image turretImg, tankImg;
	
	/**
	 * 
	 * @param i Image of this Tank, this is the tank with turret next to it
	 * @param l initial location as a Point
	 * @param slope initial Slope as a double
	 * @param speed initial speed as an int (3 is average, 0-10 is immobile to fast)
	 */
	public JakesTank(Image i, Point l, double angle, int speed) {
		super(100, true, l, i);
		tankAngle=angle;
		turretAngle=angle;
		this.speed=speed;
		
		tankImg= ((BufferedImage) i).getSubimage(0, 125, 63, 63);
		turretImg = ((BufferedImage) i).getSubimage(0, 375, 63, 63);
//		tankImg= ((BufferedImage) i).getSubimage(0, 0, 103, 66);
//		turretImg = ((BufferedImage) i).getSubimage(108, 15, 69, 36);

//		perimeter=new Perimeter(0, 0, 104, 66);
		perimeter=new Perimeter(5, 14, 52, 33);
		
		tankAffine=new AffineTransform();
//		turretAffine.scale(.5, .5);
		tankAffine.translate(l.getX(), l.getY());
		tankAffine.rotate(angle, tankCenterX, tankCenterY);
//		tankAffine.rotate(angle+Math.PI, tankCenterX, tankCenterY);
		
		turretAffine=new AffineTransform();
//		turretAffine.scale(.5, .5);
		turretAffine.translate(l.getX(), l.getY());
		turretAffine.rotate(angle, turretCenterX, turretCenterY);
//		turretAffine.rotate(angle+Math.PI, turretCenterX, turretCenterY);
	}
	
	/**
	 * angle=0,speed=3,image=default Tank, location=(300,300)
	 */
	public JakesTank(){
		super(100, true, new Point(300,300), null);
		
		try {
			tankImg= ImageIO.read(new File("images/tanks/TankSprite.png")).getSubimage(0, 125, 63, 63);
			turretImg = ImageIO.read(new File("images/tanks/TankSprite.png"))
					.getSubimage(0, 375, 63, 63);
//			tankImg= ImageIO.read(new File("images/tanks/tankOR.png")).getSubimage(0, 0, 103, 66);
//			turretImg = ImageIO.read(new File("images/tanks/tankBL.png"))
//					.getSubimage(108, 15, 69, 36);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
//		perimeter=new Perimeter(0, 0, 104, 66);
		perimeter=new Perimeter(5, 14, 52, 33);
		
		speed = 3.0;
		tankAngle = 0;
		turretAngle = 0;
		
		tankAffine=new AffineTransform();
//		tankAffine.scale(.5, .5);
		tankAffine.translate(300, 300);
//		tankAffine.rotate(Math.PI, tankCenterX, tankCenterY);
		
		turretAffine=new AffineTransform();
//		turretAffine.scale(.5, .5);
		turretAffine.translate(300, 300);
//		turretAffine.rotate(Math.PI, turretCenterX, turretCenterY);
	}
	
	public double getSpeed(){
		return speed;
	}
	
	public void increaseSpeed(double inc){
		speed += inc;
	}
	
	public AffineTransform getTankAffine(){
		return tankAffine;
	}
	
	public AffineTransform getTurretAffine(){
		return turretAffine;
	}
	
	public Shape getCollisionShape(){
		return perimeter.getRect();
	}
	
	public void scale(double xScale, double yScale){
		tankAffine.scale(xScale, yScale);
		turretAffine.scale(xScale, yScale);
	}
	
	public void moveForward(){
		tankAffine.translate(speed, 0);
		turretAffine.translate(
				speed * Math.cos(tankAngle - turretAngle),
				speed * Math.sin(tankAngle - turretAngle));
	}
	
	public void moveBackward(){
		tankAffine.translate(-speed / 2, 0);
		turretAffine.translate(
				(-speed / 2) * Math.cos(tankAngle - turretAngle),
				(-speed / 2) * Math.sin(tankAngle - turretAngle));
	}
	
	public void turnLeft(){
		tankAffine.rotate(-Math.PI / 32, 31.25, 31.25);
		tankAngle -= Math.PI / 32;
	}
	
	public void turnRight(){
		tankAffine.rotate(Math.PI / 32, 31.25, 31.25);
		tankAngle += Math.PI / 32;
	}
	
	public void turretTurnRight(){
		turretAffine.rotate(Math.PI/32, 31, 30.5);
		turretAngle+=Math.PI/32;
	}
	
	public void turretTurnLeft(){
		turretAffine.rotate(-Math.PI/32, 31, 30.5);
		turretAngle-=Math.PI/32;
	}
	
	public void adjustTurretFromMouse(MouseEvent e){
		double x, y, tempAngle = turretAngle;
		y = e.getY() - turretCenterY - turretAffine.getTranslateY();
		x = e.getX() + turretCenterX - turretAffine.getTranslateX();
		turretAngle = Math.atan(y / x);
		if (x < 0)
			turretAngle -= Math.PI;
		
		turretAffine.rotate(turretAngle - tempAngle, 31, 30.5);
	}
	
	public double getTankAngle(){
		return tankAngle;
	}
	
	public double getTurretAngle(){
		return turretAngle;
	}
	
	public Image getTurretImage(){
		return turretImg;
	}
	
	public Image getTankImage(){
		return tankImg;
	}
}
